﻿using System;
using System.Runtime.InteropServices;

namespace Toybox.CookieGetterSharp.SQLite
{
  class SQLiteDLL
  {
    #region ResultCode
    public enum Result : int
    {
      OK = 0,

      ERROR = 1,
      INTERNAL = 2,
      PERM = 3,
      ABORT = 4,
      BUSY = 5,
      LOCKED = 6,
      NOMEM = 7,
      READONLY = 8,
      INTERRUPT = 9,
      IOERR = 10,
      CORRUPT = 11,
      NOTFOUND = 12,
      FULL = 13,
      CANTOPEN = 14,
      PROTOCOL = 15,
      EMPTY = 16,
      SCHEMA = 17,
      TOOBIG = 18,
      CONSTRAINT = 19,
      MISMATCH = 20,
      MISUSE = 21,
      NOLFS = 22,
      AUTH = 23,
      FORMAT = 24,
      RANGE = 25,
      NOTADB = 26,

      ROW = 100,
      DONE = 101,
    }
    #endregion

    #region DataType
    public enum DataType : int
    {
      INTERGER = 1,
      FLOAT = 2,
      TEXT = 3,
      BLOB = 4,
      NULL = 5,
    }
    #endregion

    #region Open Close
    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_open", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_open([In, MarshalAs(UnmanagedType.LPStr)]string filename, out IntPtr db);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_open16", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_open16([In, MarshalAs(UnmanagedType.LPWStr)]string filename, out IntPtr db);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_open_v2", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_open_v2([In, MarshalAs(UnmanagedType.LPStr)]string filename, out IntPtr db, int flags, string zVfs);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_close", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_close(IntPtr db);
    #endregion

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_prepare_v2", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_prepare_v2(IntPtr db, string zSql, int nByte, out IntPtr ppStmpt, IntPtr pzTail);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_step", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_step(IntPtr stmHandle);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_finalize", CallingConvention = CallingConvention.Cdecl)]
    public static extern Result sqlite3_finalize(IntPtr stmHandle);

    //[DllImport("sqlite3.dll", EntryPoint = "sqlite3_errmsg", CallingConvention = CallingConvention.Cdecl)]
    //[return: MarshalAs(UnmanagedType.AnsiBStr)]
    //public static extern string sqlite3_errmsg(IntPtr db);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_errmsg", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr sqlite3_errmsg(IntPtr db);

    #region column
    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_count", CallingConvention = CallingConvention.Cdecl)]
    public static extern int sqlite3_column_count(IntPtr stmHandle);

    //[DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_origin_name", CallingConvention = CallingConvention.Cdecl)]
    ////[return: MarshalAs(UnmanagedType.AnsiBStr)]
    //public static extern string sqlite3_column_origin_name(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_origin_name", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr sqlite3_column_origin_name(IntPtr stmHandle, int iCol);

    //[DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_origin_name16", CallingConvention = CallingConvention.Cdecl)]
    //[return: MarshalAs(UnmanagedType.LPWStr)]
    //public static extern string sqlite3_column_origin_name16(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_origin_name16", CallingConvention = CallingConvention.Cdecl)]
    [return: MarshalAs(UnmanagedType.LPWStr)]
    public static extern IntPtr sqlite3_column_origin_name16(IntPtr stmHandle, int iCol);

    //[DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_name", CallingConvention = CallingConvention.Cdecl)]
    //[return: MarshalAs(UnmanagedType.AnsiBStr)]
    //public static extern string sqlite3_column_name(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_name", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr sqlite3_column_name(IntPtr stmHandle, int iCol);

    //[DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_name16", CallingConvention = CallingConvention.Cdecl)]
    //[return: MarshalAs(UnmanagedType.LPWStr)]
    //public static extern string sqlite3_column_name16(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_name16", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr sqlite3_column_name16(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_type", CallingConvention = CallingConvention.Cdecl)]
    public static extern DataType sqlite3_column_type(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_int", CallingConvention = CallingConvention.Cdecl)]
    public static extern int sqlite3_column_int(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_int64", CallingConvention = CallingConvention.Cdecl)]
    public static extern long sqlite3_column_int64(IntPtr stmHandle, int iCol);

    //[DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_text", CallingConvention = CallingConvention.Cdecl)]
    //[return: MarshalAs(UnmanagedType.AnsiBStr)]
    //static extern string sqlite3_column_text(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_text", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr sqlite3_column_text(IntPtr stmHandle, int iCol);

    [DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_double", CallingConvention = CallingConvention.Cdecl)]
    public static extern double sqlite3_column_double(IntPtr stmHandle, int iCol);
    #endregion
  }
}